package com.carl.leetcode.string;

/**
 * Created by carlXie on 2016/1/5.
 * email: kkrimen@gmail.com
 */
public class StringToInteger {
    public static int myAtoi(String str) {
        str = str.trim();
        int result = 0;
        boolean negative = false;
        int i = 0, len = str.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = str.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+')
                    return 0;
                if (len == 1) // Cannot have lone "+" or "-"
                    return 0;
                i++;
            }
            multmin = limit / 10;
            while (i < len) {
                digit = Character.digit(str.charAt(i++),10);
                if (digit < 0) {
                    return negative ? result : -result;
                }
                if (result < multmin) {
                    return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
                }
                result *= 10;
                if (result < limit + digit) {
                    return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
                }
                result -= digit;
            }
        }
        return negative ? result : -result;
    }

    public static void main(String[] args) {
        String str = "2147483648";
        System.out.println(myAtoi(str));
    }


}
